#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from Utils.ParseConfig import ThreadFrequency
from threading import Thread
import time


class MyThread(Thread):

    def __init__(self, func, args=()):
        super(MyThread, self).__init__()
        self.func = func
        self.args = args
        self.result = None

    def run(self):
        self.result = self.func(*self.args)

    def get_result(self):
        try:
            return self.result
        except Exception:
            return None


def multi_users(target_func, user_name, thread_number, thread_frequency=ThreadFrequency):
    threads = []
    result_list = []
    for t_number in range(thread_number):
        _user_name = user_name + str(t_number)
        t = MyThread(target_func, (_user_name, ))
        threads.append(t)
    for t in threads:
        t.start()
        time.sleep(thread_frequency)
    for t in threads:
        t.join()
        result_list.append(t.get_result())
    return result_list


if __name__ == '__main__':
    from Utils.Logger import logger
    username = "Test"
    threadnum = 2000

    def test(user_name):
        logger.info('start 1' + user_name)
        time.sleep(20)
        logger.info('start 2' + user_name)
        time.sleep(20)
        logger.info('start 3' + user_name)
        time.sleep(20)
        logger.info('start 4' + user_name)
        time.sleep(20)
        logger.info('start 5' + user_name)
        return user_name

    a = multi_users(test, 'Test', threadnum, thread_frequency=0.1)
    print(a)
